/******************************************************************************************************
Copyright (C) 2008 Mestrelab Research S.L. All rights reserved.

This file is part of the MNova scripting toolkit.

Authorized users of MNova Software may use this file freely, but this file is provided AS IS
with NO WARRANTY OF ANY KIND, INCLUDING THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE.
*****************************************************************************************************/
// This example function demonstrates how to access data points of 1D spectrum, modify them and show the result.
// It applies magnitude to all opened 1H 1D spectra. Note that this is just a demonstrative example but not the right way to magnitude the spectra.
function magnitude_each_1H_spectrum()
{
	var re = new Number;
	var im = new Number;
	var dw = new DocumentWindow(Application.mainWindow.activeWindow());
	var pageCount = dw.pageCount();
	for(var i = 0; i < pageCount; i++)
	{
		var page = new Page(dw.page(i));
		var itemCount = page.itemCount();
		for(var j = 0; j < itemCount; j++)
		{
			var spec = new NMRSpectrum(page.item(j));
			if (spec.isValid()) // if page item is a spectrum
			{
				print("Page #" + i + " Item #" + j + " is " + spec.dimCount + "D spectrum (uuid "+spec.uuid+")");
				if (spec.dimCount == 1)
				{
					if (spec.nucleus() == "1H")
					{
						nmr.beginModification(spec);
						var ptsCount = spec.count();
						for(var k = 0; k < ptsCount; k++)
						{
							if (k%1024 == 0)
							{
								page.update();
								dw.update();
							}
							re = spec.real(k);
							re *= re;
							im = spec.imag(k);
							im *= im;
							spec.setReal(k, Math.sqrt(re + im));
						}
						nmr.endModification(spec);
					}
				}
			}
			else
			{
				print("Item #" + j + " is not a spectrum.")
			}
		}
		page.update();
	}
}